package com.newbie.iSee;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.newbie.iSee.LocalDatabase.Album;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuPage extends Activity {
	private GridView gridLayout;


	public enum Function {
//		RESENT("recent", R.drawable.recent_icon, 0), 
//		FRIENDS("friends", R.drawable.friends_icon, 1), 
//		CAMARA("camera", R.drawable.camera_icon, 2), 
//		MAP("map", R.drawable.map_icon, 3), 
//		GALLERY("gallery", R.drawable.album_icon, 4), 
//		LOGIN("login", R.drawable.login_icon, 5);
		
		RESENT("recent", R.drawable.recent_icon, 0),  
		CAMARA("camera", R.drawable.camera_icon, 1), 
		MAP("map", R.drawable.map_icon, 2),  
		LOGIN("login", R.drawable.login_icon, 3);

		public final String text;
		public final int icon;
		public final int index;

		Function(String text, int icon, int index) {
			this.text = text;
			this.icon = icon;
			this.index = index;
		}

		public static Function get(int i) {
			for (Function fx : values()) {
				if (fx.index == i) {
					return fx;
				}
			}
			throw new RuntimeException("Did not find function.");
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		ShareTools.initialize(this);
		
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        


		
		gridLayout = (GridView) findViewById(R.id.menu_functions);
		gridLayout.getBackground().setDither(true);
		gridLayout.setAdapter(new ImageAdapter(this));
		gridLayout.setSelector(R.drawable.selector);

		gridLayout.setOnItemClickListener(getActions());
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		gridLayout.setAdapter(new ImageAdapter(this));
		gridLayout.setSelector(R.drawable.selector);
		gridLayout.setOnItemClickListener(getActions());		
	}

	public class ImageAdapter extends BaseAdapter {
		Context mContext;
		public static final int ACTIVITY_CREATE = 10;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return Function.values().length;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v;
			if (convertView == null) {
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.icon, null);
				TextView tv = (TextView) v.findViewById(R.id.icon_text);
				tv.setText(Function.get(position).text);
				ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
				iv.setImageResource(Function.get(position).icon);
			} else {
				v = convertView;
			}
			return v;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
	}

	private AdapterView.OnItemClickListener getActions() {
		AdapterView.OnItemClickListener actions = new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				Intent change = null;
				
				if (position == Function.RESENT.index) {
					if(ShareTools.isLogin()){

						change = new Intent(view.getContext(), Processingbar.class);
						
					}
					else {
						change = new Intent(view.getContext(), LoginPage.class);
						change.putExtra("from", "recent");
					}
				} else if (position == Function.CAMARA.index) {
					if(ShareTools.isLogin())
						change = new Intent(view.getContext(), CameraPage.class);
					else {
						change = new Intent(view.getContext(), LoginPage.class);
						change.putExtra("from", "camera");
					}
				} else if (position == Function.MAP.index) {
					if(ShareTools.isLogin())
						change = new Intent(view.getContext(), MapPage.class);
					else {
						change = new Intent(view.getContext(), LoginPage.class);
						change.putExtra("from", "map");
					}
//				} else if (position == Function.GALLERY.index) {
//					change = new Intent(view.getContext(), GalleryPage.class);
				} else if (position == Function.LOGIN.index) {
					change = new Intent(view.getContext(), LoginPage.class);
					change.putExtra("from", "login");
				} //else if (position == Function.FRIENDS.index) {
//					if(ShareTools.isLogin())
//						change = new Intent(view.getContext(), GalleryPage.class);
//					else
//						change = new Intent(view.getContext(), LoginPage.class);
//				}
				if (change != null) {
					startActivityForResult(change, 0);
				}
			}
		};
		return actions;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent evt) {
		if (keyCode != KeyEvent.KEYCODE_BACK) { // return key
			return super.onKeyDown(keyCode, evt);
		}

		finish();

		return true;
	}
}
